/*
 * EF_LLOG_Record.java
 *
 * Created on 13. November 2006, 12:49
 *
 *   This file is part of JSmex.
 *
 *  JSmex is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  JSmex is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package jsmex.function;

/**
 *
 * @author Tobias Senger
 */
public class EF_LLOG_Record {
    
    private byte status;
    private byte[] lseq = new byte[2];
    private byte wz; // Wiederholungszähler 
    private byte[] ladebetrag = new byte[3];
    private byte[] neuerbetrag = new byte[3];
    private byte[] asid = new byte[3];
    private byte[] terminalid = new byte[8];
    private byte[] tseq = new byte[3];
    private byte[] datum = new byte[4];
    private byte[] uhrzeit = new byte[3];
    private byte[] lastbseq = new byte[2];
    
    /** Creates a new instance of EF_LLOG_Record */
    public EF_LLOG_Record(byte[] data) {
        status = data[0];
        copyByteArray(data, lseq, 1,2);
        wz = data[3];
        copyByteArray(data, ladebetrag, 4,3);
        copyByteArray(data, neuerbetrag, 7,3);
        copyByteArray(data, asid, 10,3);
        copyByteArray(data, terminalid, 13, 8);
        copyByteArray(data, tseq, 21,3);
        copyByteArray(data, datum, 24,4);
        copyByteArray(data, uhrzeit, 28,3);
        copyByteArray(data, lastbseq, 31,2);
    }
    
    public int getStatus() {
        return toUnsignedInt(status);
    }
    
    public long getLSEQ() {
        return hexToLong(lseq);
    }
    
    public int getWz() {
        return toUnsignedInt(wz);
    }
    
    public String getLadeBetrag() {
        return formatBetragString( BCDByteArrayToString(ladebetrag,0,ladebetrag.length) );
    }
    
    public String getNeuerBetrag() {
        return formatBetragString( BCDByteArrayToString(neuerbetrag,0,neuerbetrag.length) );
    }  
    
    public long getASID() {
        return hexToLong(asid);
    }
    
    public String getTerminalID() {
        return BCDByteArrayToString(terminalid,0,terminalid.length);
    }
    
    public long getTSEQ() {
        return hexToLong(tseq);
    }
    
    public String getDatum() {
        StringBuffer sb = new StringBuffer( BCDByteArrayToString(datum,0,datum.length) );
        String jahr = sb.substring(0,4); 
        String monat = sb.substring(4,6);
        String tag = sb.substring(6,8);
        return tag+"."+monat+"."+jahr;
    }
    
    public String getUhrzeit() {
        StringBuffer sb = new StringBuffer( BCDByteArrayToString(uhrzeit,0,uhrzeit.length) );
        String stunde = sb.substring(0,2);
        String minute = sb.substring(2,4);
        String sekunde = sb.substring(4,6);
        return stunde+":"+minute+":"+sekunde;
    }
    
    public long getLastBSEQ() {
        return hexToLong(lastbseq);
    }
    
    
    
    private void copyByteArray(byte[] from, byte[] to, int fromstart, int length) {
        for (int i= 0;i<length;i++) to[i] = from[i+fromstart];
    }
    
    private int toUnsignedInt(byte value) {
	return (value & 0x7F) + (value < 0 ? 128 : 0);
    }
    
    private long hexToLong(byte[] ba) {
        long value = 0;
        for (int i=0;i<ba.length;i++) {
            value = value + (toUnsignedInt(ba[ba.length-1-i])*(long)Math.pow((double)256,(double)i));
        }
        return value;
    }
    
    private String formatBetragString(String betrag) {
        StringBuffer sb = new StringBuffer(betrag);
        sb.insert(4,",");
        while (sb.charAt(0)=='0') { 
            sb.deleteCharAt(0);
        }
        if (sb.charAt(0)==',') sb.insert(0,'0');
        return sb.toString();
    }
    
    private String BCDByteArrayToString(byte[] ba, int start, int end) {
         StringBuffer sb = new StringBuffer();
         byte b1;
         for (int i = start; i<end; i++) {
            b1 = ba[i];
            sb.append(toUnsignedInt(b1)>>>4);
            sb.append(toUnsignedInt(ba[i])&0x0F);
         }
         return sb.toString();
    }
    
}
